<?php

/**
 * 小班系统，获取练习筛选条件
 * Created by PhpStorm.
 * User: yaosj
 * Date: 16-5-20
 * Time: 下午6:20
 */
require __DIR__ . '/includes/utils.php';
class GetExerciseFilter implements iController {

    public function output($queryArgs) {

        if (validateUser($queryArgs) !== 1) {
            return genOutput(1000);
        }

        //处理申论点评
        resetHuping($queryArgs['user_id']);

        //获取静态过滤器
        $staticFilters = $this->getStaticExerciseFilter();

        return genOutput(1, array(
            'status_filter'     => $staticFilters['status_filter'],
            'category_filter'   => $staticFilters['category_filter'],
            'course'            => $this->getUserPurchasedCourses($queryArgs['user_id'])
        ));
    }

    /**
     * 获取静态过滤器
     *
     * @return array
     */
    private function getStaticExerciseFilter() {
        global $cmsdb;

        $cacheKey = 'func:getStaticExerciseFilter';
        if (($filters = cacheGet($cacheKey)) == null) {
            $filters = array('status_filter' => array(), 'category_filter' => array());

            //作业状态
            $filters['status_filter'][] = array('status_id' => -1, 'status_name' => '全部状态');
            $statusFilter = $cmsdb->getResults('SELECT * FROM `conf_primary_exercise_status` ORDER BY `id` ASC');
            foreach ($statusFilter as $status) {
                $filters['status_filter'][] = array('status_id' => intval($status->id), 'status_name' => $status->name);
            }

            //作业科目
            $filters['category_filter'][] = array('category_id' => -1, 'category_name' => '全部科目', 'exercise_types' => array());
            $categoryFilter = $cmsdb->getResults('SELECT `id`, `name` FROM `conf_primary_exercise_note_category` ORDER BY `id` ASC');
            $categoryTypes = $cmsdb->getResults('SELECT `id`, `name`, `note_category_id` FROM `conf_primary_exercise_type` ORDER BY `id` ASC');
            foreach ($categoryFilter as $cat) {
                $category = array('category_id' => intval($cat->id), 'category_name' => $cat->name, 'exercise_types' => array());

                $category['exercise_types'][] = array('type_id' => -1, 'type_name' => '全部类别');
                foreach ($categoryTypes as $type) {
                    if ($type->note_category_id == $cat->id)
                        $category['exercise_types'][] = array('type_id' => intval($type->id), 'type_name' => $type->name);
                }

                $filters['category_filter'][] = $category;
            }

            cacheAdd($cacheKey, $filters, 24 * 60 * 60);
        }

        return $filters;
    }


    /**
     * 获取用户已购课程
     *
     * @param int $userId 用户id
     * @return array
     */
    private function getUserPurchasedCourses($userId) {
        global $cmsdb;

        $sql = 'SELECT c.id, c.course_name FROM `data_live_course` AS c INNER JOIN `relation_user_product` AS p 
        ON c.id=p.product_id AND p.user_id=:uid AND p.product_type="zhiboke" ORDER BY p.id DESC';
        $courses = (array)$cmsdb->getResults($sql, array(':uid' => $userId));

        $list = array(array('course_id' => -1, 'course_name' => '全部课程'));
        $in = array();
        foreach ($courses as $c) {
            if (in_array($c->id, $in, true)) //滤重
                continue;

            $list[] = array('course_id' => intval($c->id), 'course_name' => $c->course_name);
            $in[] = $c->id;
        }

        return $list;
    }
}